#ifndef _ECMFS_DB_H_
#define _ECMFS_DB_H_
#include <my_global.h>
#include <mysql.h>
#include <string.h>
#include "ecmfs_cmn.h"

#define FOREIGN_KEY_MAX 17
typedef enum db_type_t_ {
	DB_TYPE_UNKNOWN,
	DB_TYPE_MASTER,
	DB_TYPE_EDGE,
	DB_TYPE_CORE,
	DB_TYPE_MAX
} db_type_t;

typedef enum db_ret_t_ {
	DB_SUCCESS=0,
	DB_ERROR=1
} db_ret_t;

typedef enum MASTER_TABLES_ {
    /*01*/ MASTER_TABLE_Node,
    /*02*/ MASTER_TABLE_NodeOpType,
    /*03*/ MASTER_TABLE_HostedFileSystem,
    /*04*/ MASTER_TABLE_FSCoreNodeMap,
    /*05*/ MASTER_TABLE_FSEdgeNodeMap,
    /*05*/ MASTER_TABLE_CoreEdgeMap,
    /*06*/ MASTER_TABLE_User,
    /*07*/ MASTER_TABLE_UserFSEdgeMap,
    /*08*/ MASTER_TABLE_UserFSCoreMap,
    /*09*/ MASTER_TABLE_FSGroup, 
    /*10*/ MASTER_TABLE_FSGrpUserMap, 
    /*11*/ MASTER_TABLE_File,
    /*12*/ MASTER_TABLE_FSGroupFileMap,
    /*14*/ MASTER_TABLE_Session,
    /*15*/ MASTER_TABLE_MAX
}MASTER_TABLES_T;

typedef struct db_conn_ {
    const char *host;
    const char *login;
    const char *password;
    const char *name;
    MYSQL *con;
} db_conn_t;
typedef struct node_details_ {
    char node_name[32];
    char node_ip[32];
    int port;
    int node_type;
    int node_id;
} node_details_t;
void finish_with_error(void);
void createConn(int dbFlag);
void closeConn(void);
void init_params(db_type_t db_type);
void conn_info();
int createDb();
int createTables();
db_ret_t create_db(char *db_name);
db_ret_t open_db();
db_ret_t close_db();
db_ret_t add_user(char *user, char *password);
db_ret_t del_user(char *user);
db_ret_t add_node(node_info_t *node_info);
db_ret_t check_user(char *user); 
db_ret_t check_node(char *node);   
db_ret_t get_nodes(node_details_t **nodes,
                   int *node_count);
#endif
